<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="WKWebView WKWebView 有两个Delegate,WKUIDelegate和WKNavigationDelegate,WKNavigationDelegate主要处理一些跳转,加载,WKUIDelegate主要处理JS脚本,确认框,警告框等等…">
<meta property="og:type" content="article">
<meta property="og:title" content="WKWebView">
<meta property="og:url" content="http://yoursite.com/2018/10/17/WKWebView/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="WKWebView WKWebView 有两个Delegate,WKUIDelegate和WKNavigationDelegate,WKNavigationDelegate主要处理一些跳转,加载,WKUIDelegate主要处理JS脚本,确认框,警告框等等…">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws1.sinaimg.cn/large/006tNbRwly1fwba2ryjouj315k0rgn5l.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwba3btb60j31am01qdgb.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwba3sflwpj31kw0nfnj0.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwba4h9u2aj312o0euafk.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/large/006tNbRwly1fwba4tw44pj31kw0sfaqz.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tNbRwly1fwba5i7t49j31kw0n4tpt.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwba5vuymlj31kw0nkk82.jpg">
<meta property="og:updated_time" content="2019-07-19T05:40:06.077Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="WKWebView">
<meta name="twitter:description" content="WKWebView WKWebView 有两个Delegate,WKUIDelegate和WKNavigationDelegate,WKNavigationDelegate主要处理一些跳转,加载,WKUIDelegate主要处理JS脚本,确认框,警告框等等…">
<meta name="twitter:image" content="https://ws1.sinaimg.cn/large/006tNbRwly1fwba2ryjouj315k0rgn5l.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/17/WKWebView/"/>





  <title>WKWebView | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/17/WKWebView/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">WKWebView</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-10-17T08:44:15+08:00">
                2018-10-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/WKWebView/" itemprop="url" rel="index">
                    <span itemprop="name">WKWebView</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/10/17/WKWebView/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/10/17/WKWebView/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="WKWebView"><a href="#WKWebView" class="headerlink" title="WKWebView"></a>WKWebView</h3><blockquote>
<p><strong>WKWebView 有两个Delegate,WKUIDelegate和WKNavigationDelegate,WKNavigationDelegate主要处理一些跳转,加载,WKUIDelegate主要处理JS脚本,确认框,警告框等等…</strong><a id="more"></a></p>
</blockquote>
<h4 id="WKWebViewConfiguration"><a href="#WKWebViewConfiguration" class="headerlink" title="WKWebViewConfiguration"></a>WKWebViewConfiguration</h4><blockquote>
<p><strong>WKWebViewConfiguration是专门用来配置webView</strong></p>
</blockquote>
<h4 id="WKPreference"><a href="#WKPreference" class="headerlink" title="WKPreference"></a>WKPreference</h4><blockquote>
<p><strong>WKPreference是用来进行相关webView设置</strong></p>
</blockquote>
<h4 id="WKProcessPool"><a href="#WKProcessPool" class="headerlink" title="WKProcessPool"></a>WKProcessPool</h4><blockquote>
<p><strong>WKProcessPool是用来配置进程池,与网页视图的资源共享相关</strong></p>
</blockquote>
<h4 id="WKUserContentController"><a href="#WKUserContentController" class="headerlink" title="WKUserContentController"></a>WKUserContentController</h4><blockquote>
<p><strong>WKUserContentController是用来做native(原生)与JavaScript相互调用</strong></p>
</blockquote>
<h4 id="WKUserScript"><a href="#WKUserScript" class="headerlink" title="WKUserScript"></a>WKUserScript</h4><blockquote>
<p><strong>WKUserScript是用来进行JavaScript注入</strong></p>
</blockquote>
<h4 id="WKScriptMessageHandler"><a href="#WKScriptMessageHandler" class="headerlink" title="WKScriptMessageHandler"></a>WKScriptMessageHandler</h4><blockquote>
<p><strong>WKScriptMessageHandler是用来专门处理JavaScript调用Native(原生)的方法</strong></p>
</blockquote>
<h4 id="WKBackForwardList"><a href="#WKBackForwardList" class="headerlink" title="WKBackForwardList"></a>WKBackForwardList</h4><blockquote>
<p><strong>WKBackForwardList栈堆管理的网页列表</strong></p>
</blockquote>
<h4 id="WKBackForwardListItem"><a href="#WKBackForwardListItem" class="headerlink" title="WKBackForwardListItem"></a>WKBackForwardListItem</h4><blockquote>
<p><strong>WKBackForwardListItem每一个网页节点对象</strong></p>
</blockquote>
<h4 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h4><h5 id="配置-只读初始化时可以设置"><a href="#配置-只读初始化时可以设置" class="headerlink" title="配置,只读初始化时可以设置"></a>配置,只读初始化时可以设置</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly, copy) WKWebViewConfiguration *configuration;</span><br></pre></td></tr></table></figure>
<h5 id="导航代理"><a href="#导航代理" class="headerlink" title="导航代理"></a>导航代理</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nullable, nonatomic, weak)id navigationDelegate;</span><br></pre></td></tr></table></figure>
<h5 id="UI代理"><a href="#UI代理" class="headerlink" title="UI代理"></a>UI代理</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nullable, nonatomic, weak)id UIDelegate;</span><br></pre></td></tr></table></figure>
<h5 id="访问过网页历史列表"><a href="#访问过网页历史列表" class="headerlink" title="访问过网页历史列表"></a>访问过网页历史列表</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly, strong) WKBackForwardList *backForwardList;</span><br></pre></td></tr></table></figure>
<h5 id="网页标题"><a href="#网页标题" class="headerlink" title="网页标题"></a>网页标题</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nullable, nonatomic,readonly, copy) NSString *title;</span><br></pre></td></tr></table></figure>
<h5 id="网页的URL地址"><a href="#网页的URL地址" class="headerlink" title="网页的URL地址"></a>网页的URL地址</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nullable, nonatomic,readonly, copy) NSURL *URL;</span><br></pre></td></tr></table></figure>
<h5 id="网页是否正在加载"><a href="#网页是否正在加载" class="headerlink" title="网页是否正在加载"></a>网页是否正在加载</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly, getter=isLoading) BOOL loading;</span><br></pre></td></tr></table></figure>
<h5 id="加载的进度-范围为-0-1"><a href="#加载的进度-范围为-0-1" class="headerlink" title="加载的进度 范围为[0, 1]"></a>加载的进度 范围为[0, 1]</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly)double estimatedProgress;</span><br></pre></td></tr></table></figure>
<h5 id="网页链接是否安全"><a href="#网页链接是否安全" class="headerlink" title="网页链接是否安全"></a>网页链接是否安全</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly) BOOL hasOnlySecureContent;</span><br></pre></td></tr></table></figure>
<h5 id="证书服务"><a href="#证书服务" class="headerlink" title="证书服务"></a>证书服务</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly, nullable) SecTrustRef serverTrust API_AVAILABLE(macosx(10.12), ios(10.0));</span><br></pre></td></tr></table></figure>
<h5 id="是否可以返回"><a href="#是否可以返回" class="headerlink" title="是否可以返回"></a>是否可以返回</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly) BOOL canGoBack;</span><br></pre></td></tr></table></figure>
<h5 id="是否可以前进"><a href="#是否可以前进" class="headerlink" title="是否可以前进"></a>是否可以前进</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly) BOOL canGoForward;</span><br></pre></td></tr></table></figure>
<h5 id="自定义代理字符串"><a href="#自定义代理字符串" class="headerlink" title="自定义代理字符串"></a>自定义代理字符串</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nullable, nonatomic, copy) NSString *customUserAgent API_AVAILABLE(macosx(10.11), ios(9.0));</span><br></pre></td></tr></table></figure>
<h5 id="在iOS上默认为NO，标识不允许链接预览"><a href="#在iOS上默认为NO，标识不允许链接预览" class="headerlink" title="在iOS上默认为NO，标识不允许链接预览"></a>在iOS上默认为NO，标识不允许链接预览</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic) BOOL allowsLinkPreview API_AVAILABLE(macosx(10.11), ios(9.0));</span><br></pre></td></tr></table></figure>
<h5 id="滚动视图"><a href="#滚动视图" class="headerlink" title="滚动视图"></a>滚动视图</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic,readonly, strong) UIScrollView *scrollView;</span><br></pre></td></tr></table></figure>
<h5 id="是否支持放大手势-默认为NO"><a href="#是否支持放大手势-默认为NO" class="headerlink" title="是否支持放大手势,默认为NO"></a>是否支持放大手势,默认为NO</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic) BOOL allowsMagnification;</span><br></pre></td></tr></table></figure>
<h5 id="放大因子，默认为1"><a href="#放大因子，默认为1" class="headerlink" title="放大因子，默认为1"></a>放大因子，默认为1</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic) CGFloat magnification;</span><br></pre></td></tr></table></figure>
<hr>
<h4 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h4><h5 id="加载webView"><a href="#加载webView" class="headerlink" title="加载webView"></a>加载webView</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable <span class="type">WKNavigation</span> *)loadRequest:(<span class="type">NSURLRequest</span> *)request;</span><br></pre></td></tr></table></figure>
<h5 id="用字符串加载"><a href="#用字符串加载" class="headerlink" title="用字符串加载"></a>用字符串加载</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable <span class="type">WKNavigation</span> *)loadHTMLString:(<span class="type">NSString</span> *)string baseURL:(nullable <span class="type">NSURL</span> *)baseURL;</span><br></pre></td></tr></table></figure>
<h5 id="NSData加载"><a href="#NSData加载" class="headerlink" title="NSData加载"></a>NSData加载</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable <span class="type">WKNavigation</span> *)loadData:(<span class="type">NSData</span> *)data <span class="type">MIMEType</span>:(<span class="type">NSString</span> *)<span class="type">MIMEType</span> characterEncodingName:(<span class="type">NSString</span> *)characterEncodingName baseURL:(<span class="type">NSURL</span> *)baseURL <span class="type">API_AVAILABLE</span>(macosx(<span class="number">10.11</span>), ios(<span class="number">9.0</span>));</span><br></pre></td></tr></table></figure>
<h5 id="返回上一个节点"><a href="#返回上一个节点" class="headerlink" title="返回上一个节点"></a>返回上一个节点</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;</span><br></pre></td></tr></table></figure>
<h5 id="返回到上一个网页"><a href="#返回到上一个网页" class="headerlink" title="返回到上一个网页"></a>返回到上一个网页</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable WKNavigation *)goBack;</span><br></pre></td></tr></table></figure>
<h5 id="前进到下一个网页"><a href="#前进到下一个网页" class="headerlink" title="前进到下一个网页"></a>前进到下一个网页</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable WKNavigation *)goForward;</span><br></pre></td></tr></table></figure>
<h5 id="重新加载"><a href="#重新加载" class="headerlink" title="重新加载"></a>重新加载</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable WKNavigation *)reload;</span><br></pre></td></tr></table></figure>
<h5 id="忽略缓存-重新加载"><a href="#忽略缓存-重新加载" class="headerlink" title="忽略缓存 重新加载"></a>忽略缓存 重新加载</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable WKNavigation *)reloadFromOrigin;</span><br></pre></td></tr></table></figure>
<h5 id="停止加载"><a href="#停止加载" class="headerlink" title="停止加载"></a>停止加载</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)stopLoading;</span><br></pre></td></tr></table></figure>
<h5 id="执行JavaScript"><a href="#执行JavaScript" class="headerlink" title="执行JavaScript"></a>执行JavaScript</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void(^ _Nullable)(_Nullableid, NSError * _Nullable error))completionHandler;</span><br></pre></td></tr></table></figure>
<h5 id="是否允许左右滑动，返回-前进操作-默认是NO"><a href="#是否允许左右滑动，返回-前进操作-默认是NO" class="headerlink" title="是否允许左右滑动，返回-前进操作  默认是NO"></a>是否允许左右滑动，返回-前进操作  默认是NO</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic) BOOL allowsBackForwardNavigationGestures;</span><br></pre></td></tr></table></figure>
<h5 id="据设置的缩放因子来缩放页面，并居中显示结果在指定的点"><a href="#据设置的缩放因子来缩放页面，并居中显示结果在指定的点" class="headerlink" title="据设置的缩放因子来缩放页面，并居中显示结果在指定的点"></a>据设置的缩放因子来缩放页面，并居中显示结果在指定的点</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;/// 证书列表@property (nonatomic,readonly, copy) NSArray *certificateChain API_DEPRECATED_WITH_REPLACEMENT(&quot;serverTrust&quot;, macosx(10.11,10.12), ios(9.0,10.0));</span><br></pre></td></tr></table></figure>
<hr>
<h4 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">WKWebView</span> *webView = [[<span class="type">WKWebView</span> alloc] initWithFrame:<span class="keyword">self</span>.view.bounds];</span><br><span class="line">webView.<span class="type">UIDelegate</span> = <span class="keyword">self</span>;</span><br><span class="line">webView.navigationDelegate = <span class="keyword">self</span>;</span><br><span class="line">[<span class="keyword">self</span>.view addSubview:webView];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 允许手势返回上一页</span></span><br><span class="line">[webView setAllowsBackForwardNavigationGestures:<span class="literal">true</span>];</span><br></pre></td></tr></table></figure>
<h4 id="WKNavigationDelegate"><a href="#WKNavigationDelegate" class="headerlink" title="WKNavigationDelegate"></a>WKNavigationDelegate</h4><h5 id="页面开始加载时调用"><a href="#页面开始加载时调用" class="headerlink" title="页面开始加载时调用"></a>页面开始加载时调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView didStartProvisionalNavigation:(<span class="type">WKNavigation</span> *)navigation;</span><br></pre></td></tr></table></figure>
<h5 id="当内容开始返回时调用"><a href="#当内容开始返回时调用" class="headerlink" title="当内容开始返回时调用"></a>当内容开始返回时调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView didCommitNavigation:(<span class="type">WKNavigation</span> *)navigation;</span><br></pre></td></tr></table></figure>
<h5 id="页面加载完成之后调用"><a href="#页面加载完成之后调用" class="headerlink" title="页面加载完成之后调用"></a>页面加载完成之后调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView didFinishNavigation:(<span class="type">WKNavigation</span> *)navigation</span><br></pre></td></tr></table></figure>
<h5 id="页面加载失败时调用"><a href="#页面加载失败时调用" class="headerlink" title="页面加载失败时调用"></a>页面加载失败时调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView didFailProvisionalNavigation:(<span class="type">WKNavigation</span> *)navigation;</span><br></pre></td></tr></table></figure>
<h5 id="接收到服务器跳转请求之后调用"><a href="#接收到服务器跳转请求之后调用" class="headerlink" title="接收到服务器跳转请求之后调用"></a>接收到服务器跳转请求之后调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView didReceiveServerRedirectForProvisionalNavigation:(<span class="type">WKNavigation</span> *)navigation;</span><br></pre></td></tr></table></figure>
<h5 id="在收到响应后-决定是否跳转"><a href="#在收到响应后-决定是否跳转" class="headerlink" title="在收到响应后,决定是否跳转"></a>在收到响应后,决定是否跳转</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView decidePolicyForNavigationResponse:(<span class="type">WKNavigationResponse</span> *)navigationResponse decisionHandler:(void (^)(<span class="type">WKNavigationResponsePolicy</span>))decisionHandler&#123;</span><br><span class="line"> </span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">"%@"</span>,navigationResponse.response.<span class="type">URL</span>.absoluteString);</span><br><span class="line">    <span class="comment">//允许跳转</span></span><br><span class="line">    decisionHandler(<span class="type">WKNavigationResponsePolicyAllow</span>);</span><br><span class="line">    <span class="comment">//不允许跳转</span></span><br><span class="line">    <span class="comment">//decisionHandler(WKNavigationResponsePolicyCancel);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="在发送请求之前-决定是否跳转"><a href="#在发送请求之前-决定是否跳转" class="headerlink" title="在发送请求之前,决定是否跳转"></a>在发送请求之前,决定是否跳转</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler&#123;</span><br><span class="line"> </span><br><span class="line">     NSLog(@&quot;%@&quot;,navigationAction.request.URL.absoluteString);</span><br><span class="line">    //允许跳转</span><br><span class="line">    decisionHandler(WKNavigationActionPolicyAllow);</span><br><span class="line">    //不允许跳转</span><br><span class="line">    //decisionHandler(WKNavigationActionPolicyCancel);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="点击网页没反应的解决方法"><a href="#点击网页没反应的解决方法" class="headerlink" title="点击网页没反应的解决方法"></a>点击网页没反应的解决方法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;a href=&quot;xxx.jsp&quot; target=&quot;_blank&quot;/&gt;  这个target参数是_blank</span><br><span class="line">- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler&#123;</span><br><span class="line">    </span><br><span class="line">    //需要判断targetFrame是否为nil，如果为空则重新请求</span><br><span class="line">    if (navigationAction.targetFrame == nil) &#123;</span><br><span class="line">        [self.webView loadRequest:navigationAction.request];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    decisionHandler(WKNavigationActionPolicyAllow);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="WKUIDelegate"><a href="#WKUIDelegate" class="headerlink" title="WKUIDelegate"></a>WKUIDelegate</h4><h5 id="创建新的webView时调用的方法"><a href="#创建新的webView时调用的方法" class="headerlink" title="创建新的webView时调用的方法"></a>创建新的webView时调用的方法</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="type">WKWebView</span> *)webView:(<span class="type">WKWebView</span> *)webView createWebViewWithConfiguration:(<span class="type">WKWebViewConfiguration</span> *)configuration forNavigationAction:(<span class="type">WKNavigationAction</span> *)navigationAction windowFeatures:(<span class="type">WKWindowFeatures</span> *)windowFeatures</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> [[<span class="type">WKWebView</span> alloc]<span class="keyword">init</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="输入框-JavaScript调用prompt方法后回调的方法-prompt是js中的输入框-需要在block中把用户输入的信息传入"><a href="#输入框-JavaScript调用prompt方法后回调的方法-prompt是js中的输入框-需要在block中把用户输入的信息传入" class="headerlink" title="输入框,JavaScript调用prompt方法后回调的方法 prompt是js中的输入框 需要在block中把用户输入的信息传入"></a>输入框,JavaScript调用prompt方法后回调的方法 prompt是js中的输入框 需要在block中把用户输入的信息传入</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView runJavaScriptTextInputPanelWithPrompt:(<span class="type">NSString</span> *)prompt defaultText:(nullable <span class="type">NSString</span> *)defaultText initiatedByFrame:(<span class="type">WKFrameInfo</span> *)frame completionHandler:(void (^)(<span class="type">NSString</span> * __nullable result))completionHandler&#123;</span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">"%@"</span>,prompt);</span><br><span class="line">    completionHandler(@<span class="string">"1234"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="确认框-JavaScript调用confirm方法后回调的方法-confirm是js中的确定框，需要在block中把用户选择的情况传递进去"><a href="#确认框-JavaScript调用confirm方法后回调的方法-confirm是js中的确定框，需要在block中把用户选择的情况传递进去" class="headerlink" title="确认框,JavaScript调用confirm方法后回调的方法 confirm是js中的确定框，需要在block中把用户选择的情况传递进去"></a>确认框,JavaScript调用confirm方法后回调的方法 confirm是js中的确定框，需要在block中把用户选择的情况传递进去</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler&#123;</span><br><span class="line">    completionHandler(YES);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="警告框-web界面中有弹出警告框时调用"><a href="#警告框-web界面中有弹出警告框时调用" class="headerlink" title="警告框,web界面中有弹出警告框时调用"></a>警告框,web界面中有弹出警告框时调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">*  @param webView           实现该代理的webview</span><br><span class="line">*  @param message           警告框中的内容</span><br><span class="line">*  @param completionHandler 警告框消失调用</span><br><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView runJavaScriptAlertPanelWithMessage:(<span class="type">NSString</span> *)message initiatedByFrame:(<span class="type">WKFrameInfo</span> *)frame completionHandler:(void (^)(void))completionHandler&#123;</span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">"%@"</span>,message);</span><br><span class="line">    completionHandler();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="默认浏览元素调用"><a href="#默认浏览元素调用" class="headerlink" title="默认浏览元素调用"></a>默认浏览元素调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="type">BOOL</span>)webView:(<span class="type">WKWebView</span> *)webView shouldPreviewElement:(<span class="type">WKPreviewElementInfo</span> *)elementInfo &#123;</span><br><span class="line"></span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">"-----默认预览元素调用"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="type">YES</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="关闭WebView时调用"><a href="#关闭WebView时调用" class="headerlink" title="关闭WebView时调用"></a>关闭WebView时调用</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)webViewDidClose:(<span class="type">WKWebView</span> *)webVie</span><br></pre></td></tr></table></figure>
<h5 id="返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为"><a href="#返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为" class="headerlink" title="返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为"></a>返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- (nullable <span class="type">UIViewController</span> *)webView:(<span class="type">WKWebView</span> *)webView previewingViewControllerForElement:(<span class="type">WKPreviewElementInfo</span> *)elementInfo defaultActions:(<span class="type">NSArray</span>&lt;id &lt;<span class="type">WKPreviewActionItem</span>&gt;&gt; *)previewActions &#123;</span><br><span class="line"></span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">"----返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为。"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="允许应用程序向它创建的视图控制器弹出"><a href="#允许应用程序向它创建的视图控制器弹出" class="headerlink" title="允许应用程序向它创建的视图控制器弹出"></a>允许应用程序向它创建的视图控制器弹出</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(<span class="type">WKWebView</span> *)webView commitPreviewingViewController:(<span class="type">UIViewController</span> *)previewingViewController &#123;</span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">"----允许应用程序向它创建的视图控制器弹出"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="监听加载进度"><a href="#监听加载进度" class="headerlink" title="监听加载进度"></a>监听加载进度</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">UIProgressView *_progressView;</span><br><span class="line">_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame),2)];</span><br><span class="line">_progressView.tintColor = [UIColor redColor];</span><br><span class="line">[self.view addSubview:_progressView];</span><br><span class="line">//观察者</span><br><span class="line">[_webView addObserver:self forKeyPath:@&quot;estimatedProgress&quot; options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context&#123;</span><br><span class="line"></span><br><span class="line">    if ([keyPath isEqual: @&quot;estimatedProgress&quot;] &amp;&amp; object == _webView) &#123;</span><br><span class="line">        [_progressView setAlpha:1.0f];</span><br><span class="line">        [_progressView setProgress:_webView.estimatedProgress animated:YES];</span><br><span class="line">        if(_webView.estimatedProgress &gt;= 1.0f)</span><br><span class="line">        &#123;</span><br><span class="line">            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^&#123;</span><br><span class="line">                [self-&gt;_progressView setAlpha:0.0f];</span><br><span class="line">            &#125; completion:^(BOOL finished) &#123;</span><br><span class="line">                [self-&gt;_progressView setProgress:0.0f animated:NO];</span><br><span class="line">            &#125;];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    else &#123;</span><br><span class="line">        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark - 销毁</span><br><span class="line">- (void)dealloc &#123;</span><br><span class="line">    [_webView removeObserver:self forKeyPath:@&quot;estimatedProgress&quot;];</span><br><span class="line">    [_webView setNavigationDelegate:nil];</span><br><span class="line">    [_webView setUIDelegate:nil];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="WKUserContentController-1"><a href="#WKUserContentController-1" class="headerlink" title="WKUserContentController"></a>WKUserContentController</h3><h4 id="核心两个方法"><a href="#核心两个方法" class="headerlink" title="核心两个方法"></a>核心两个方法</h4><h5 id="向请求的页面中注入JS代码，如果需要在请求的页面中加入自己的JS代码，用此方法"><a href="#向请求的页面中注入JS代码，如果需要在请求的页面中加入自己的JS代码，用此方法" class="headerlink" title="向请求的页面中注入JS代码，如果需要在请求的页面中加入自己的JS代码，用此方法"></a>向请求的页面中注入JS代码，如果需要在请求的页面中加入自己的JS代码，用此方法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)addUserScript:(WKUserScript *)userScript;</span><br></pre></td></tr></table></figure>
<h5 id="JavaScript调用OC的桥梁"><a href="#JavaScript调用OC的桥梁" class="headerlink" title="JavaScript调用OC的桥梁"></a>JavaScript调用OC的桥梁</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (void)addScriptMessageHandler:(id &lt;WKScriptMessageHandler&gt;)scriptMessageHandler name:(NSString *)nam</span><br></pre></td></tr></table></figure>
<h4 id="WKScriptMessage"><a href="#WKScriptMessage" class="headerlink" title="WKScriptMessage"></a>WKScriptMessage</h4><blockquote>
<p><strong>WKScriptMessage中有两个用到的属性一个是name,一个是body,name是约定好的函数名</strong></p>
</blockquote>
<h5 id="JS想通知OC只需要"><a href="#JS想通知OC只需要" class="headerlink" title="JS想通知OC只需要"></a>JS想通知OC只需要</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">window.webkit.messageHandlers.约定好的名字.postMessage(参数)</span><br></pre></td></tr></table></figure>
<h4 id="JS调用OC"><a href="#JS调用OC" class="headerlink" title="JS调用OC"></a>JS调用OC</h4><p><strong>初始化</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">//  创建配置</span><br><span class="line">WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];</span><br><span class="line">//  创建WKUserContentController</span><br><span class="line">WKUserContentController *wkucc = [[WKUserContentController alloc]init];</span><br><span class="line">//  将创建好的WKUserContentController对象赋值给配置对象</span><br><span class="line">config.userContentController = wkucc;</span><br><span class="line">//  设置约定函数名 就是jsCallOC</span><br><span class="line">[config.userContentController addScriptMessageHandler:self name:@&quot;jsCallOC&quot;];</span><br><span class="line">//  使用配置信息初始化webView</span><br><span class="line">_webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//  路径</span><br><span class="line">NSString *fileURL = [[NSBundle mainBundle] pathForResource:@&quot;index&quot; ofType:@&quot;html&quot;];</span><br><span class="line">//  路径 → URL</span><br><span class="line">NSURL *url = [NSURL fileURLWithPath:fileURL];</span><br><span class="line">//  加载webView</span><br><span class="line">[_webView loadRequest:[NSURLRequest requestWithURL:url]];</span><br><span class="line">//  添加到屏幕上</span><br><span class="line">[self.view addSubview:_webView];</span><br></pre></td></tr></table></figure>
<p><strong>实现WKScriptMessageHandler协议方法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">// &lt;WKScriptMessageHandler&gt;</span><br><span class="line">//JS中调用window.webkit.messageHandlers.jsCallOC.postMessage(dict);会执行下边的方法</span><br><span class="line">-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message&#123;</span><br><span class="line">    NSLog(@&quot;%@---%@&quot;,message.name,message.body);</span><br><span class="line">    这里的name就是上面定义的jsCallOC,message是JS那边传过来的参数</span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line">     // 方法名</span><br><span class="line">    NSString *methods = [NSString stringWithFormat:@&quot;%@:&quot;, message.name];</span><br><span class="line">    SEL selector = NSSelectorFromString(methods);</span><br><span class="line">    // 调用方法</span><br><span class="line">    if ([self respondsToSelector:selector]) &#123;</span><br><span class="line">        [self performSelector:selector withObject:message.body];</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        NSLog(@&quot;未实行方法：%@&quot;, methods);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>index.html代码</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;meta http-equiv=<span class="string">"content-type"</span> content=<span class="string">"text/html;charset=utf-8"</span>&gt;&lt;/meta&gt;</span><br><span class="line">&lt;title&gt;Test&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        function displayDate()&#123;</span></span><br><span class="line"><span class="regexp">            document.getElementById("demo").innerHTML=Date();</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        function onClickButton() &#123;</span></span><br><span class="line"><span class="regexp">        var list = [1,2,3];</span></span><br><span class="line"><span class="regexp">        var dict = &#123;"name":"阳君", "qq":"937447974"&#125;;</span></span><br><span class="line"><span class="regexp">        /</span><span class="regexp">/ JS通知WKWebView</span></span><br><span class="line"><span class="regexp">        /</span><span class="regexp">/ 2 在这通知OC</span></span><br><span class="line"><span class="regexp">        window.webkit.messageHandlers.jsCallOC.postMessage(dict);</span></span><br><span class="line"><span class="regexp">        </span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        function ocCallJS(params)&#123;</span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp">        </span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp">    </span></span><br><span class="line"><span class="regexp">    &lt;/</span>script&gt;</span><br><span class="line">&lt;<span class="regexp">/head&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;body&gt;</span></span><br><span class="line"><span class="regexp">&lt;p id="demo"&gt;这是一个段落&lt;/</span>p&gt;</span><br><span class="line">&lt;button type=<span class="string">"button"</span> onclick=<span class="string">"onClickButton()"</span>&gt;显示日期&lt;<span class="regexp">/button&gt;	/</span><span class="regexp">/1 当按钮被点击调用onClickButton函数</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line">&lt;<span class="regexp">/html&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="OC调用JS"><a href="#OC调用JS" class="headerlink" title="OC调用JS"></a>OC调用JS</h4><p><strong>比如js中的函数为</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// WKWebView调用JS</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ocCallJS</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">    show.innerHTML = params;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>OC调用JS</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dic = @&#123;@&quot;name&quot; : @&quot;zhangsan&quot;,@&quot;age&quot; : @&quot;18&quot;&#125;;</span><br><span class="line">/*po jsString</span><br><span class="line"> ocCallJS(&apos;&#123;</span><br><span class="line"> age = 18;</span><br><span class="line"> name = zhangsan;</span><br><span class="line"> &#125;&apos;*/</span><br><span class="line">NSString *jsString = [NSString stringWithFormat:@&quot;ocCallJS(&apos;%@&apos;)&quot;,dic];</span><br><span class="line">[_webView evaluateJavaScript:jsString completionHandler:nil];</span><br></pre></td></tr></table></figure>
<h4 id="WKUserScript-JS注入"><a href="#WKUserScript-JS注入" class="headerlink" title="WKUserScript JS注入"></a>WKUserScript JS注入</h4><p><strong>在webkit框架中,我们还可以预先添加js方法,供其他人员调用,WKUserScript就是帮助我们完成JS注入的类,这个类帮助我们在页面填充前或JS填充完成后调用,核心方法</strong></p>
<h5 id="WKUserScriptInjectionTime-枚举-注入的时间"><a href="#WKUserScriptInjectionTime-枚举-注入的时间" class="headerlink" title="WKUserScriptInjectionTime 枚举 注入的时间"></a>WKUserScriptInjectionTime 枚举 注入的时间</h5><h6 id="WKUserScriptInjectionTimeAtDocumentStart：js加载前执行"><a href="#WKUserScriptInjectionTimeAtDocumentStart：js加载前执行" class="headerlink" title="WKUserScriptInjectionTimeAtDocumentStart：js加载前执行"></a>WKUserScriptInjectionTimeAtDocumentStart：js加载前执行</h6><h6 id="WKUserScriptInjectionTimeAtDocumentEnd：js加载后执行"><a href="#WKUserScriptInjectionTimeAtDocumentEnd：js加载后执行" class="headerlink" title="WKUserScriptInjectionTimeAtDocumentEnd：js加载后执行"></a>WKUserScriptInjectionTimeAtDocumentEnd：js加载后执行</h6><h5 id="注入"><a href="#注入" class="headerlink" title="注入"></a>注入</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">- (WKWebView *)webView &#123;</span><br><span class="line">    if (_webView == nil) &#123;</span><br><span class="line">        // js配置</span><br><span class="line">        WKUserContentController *userContentController = [[WKUserContentController alloc] init];</span><br><span class="line">        [userContentController addScriptMessageHandler:self name:@&quot;jsCallOC&quot;];</span><br><span class="line">        // js注入，注入一个alert方法，页面加载完毕弹出一个对话框。</span><br><span class="line">        NSString *javaScriptSource = @&quot;alert(\&quot;WKUserScript注入js\&quot;);&quot;;</span><br><span class="line">        //	创建注入类</span><br><span class="line">        WKUserScript *userScript = [[WKUserScript alloc] initWithSource:javaScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];// forMainFrameOnly:NO(全局窗口)，yes（只限主窗口）</span><br><span class="line">        [userContentController addUserScript:userScript];</span><br><span class="line"></span><br><span class="line">        // WKWebView的配置</span><br><span class="line">        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];</span><br><span class="line">        configuration.userContentController = userContentController;</span><br><span class="line"></span><br><span class="line">        // 显示WKWebView</span><br><span class="line">        _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];</span><br><span class="line">        [self.view addSubview:_webView];</span><br><span class="line">    &#125;</span><br><span class="line">    return _webView;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)dealloc&#123;</span><br><span class="line">//这里需要注意，前面增加过的方法一定要remove掉。</span><br><span class="line">[userContentController removeScriptMessageHandlerForName:@&quot;ocCallJS&quot;];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h4 id="webView中删除广告"><a href="#webView中删除广告" class="headerlink" title="webView中删除广告"></a>webView中删除广告</h4><p><strong>这怎么写?还是看图吧</strong></p>
<p><img src="https://ws1.sinaimg.cn/large/006tNbRwly1fwba2ryjouj315k0rgn5l.jpg" alt=""></p>
<p><strong>这里选择  开发者工具</strong></p>
<p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwba3btb60j31am01qdgb.jpg" alt=""></p>
<p><strong>然后选择这两个</strong></p>
<p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwba3sflwpj31kw0nfnj0.jpg" alt=""></p>
<p><strong>点击定位代码,展开代码</strong></p>
<p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwba4h9u2aj312o0euafk.jpg" alt=""></p>
<p><strong>找到Class</strong></p>
<p><img src="https://ws1.sinaimg.cn/large/006tNbRwly1fwba4tw44pj31kw0sfaqz.jpg" alt=""></p>
<p><strong>在Console写一下看看能不能隐藏</strong></p>
<p><img src="https://ws2.sinaimg.cn/large/006tNbRwly1fwba5i7t49j31kw0n4tpt.jpg" alt=""></p>
<p><strong>同理隐藏下一个</strong></p>
<p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwba5vuymlj31kw0nkk82.jpg" alt=""></p>
<p><strong>成功之后返回Xcode,找啊个合适的地方写隐藏代码</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark webDelegate</span><br><span class="line">- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation&#123;</span><br><span class="line">    </span><br><span class="line">[_webView evaluateJavaScript:@&quot;javascript:document.getElementsByClassName(&apos;cc-message&apos;)[0].style.display = &apos;none&apos;&quot; completionHandler:nil];</span><br><span class="line">[_webView evaluateJavaScript:@&quot;document.getElementsByClassName(&apos;cc-compliance&apos;)[0].style.display=&apos;none&apos;&quot; completionHandler:nil];</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取Cookie"><a href="#获取Cookie" class="headerlink" title="获取Cookie"></a>获取Cookie</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler&#123;</span><br><span class="line"></span><br><span class="line">    if (self.cookie == nil) &#123;</span><br><span class="line">        NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;</span><br><span class="line">        NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];</span><br><span class="line">        //读取wkwebview中的cookie</span><br><span class="line">        for (NSHTTPCookie *cookie in cookies) &#123;</span><br><span class="line">            NSLog(@&quot;cookie ========= %@&quot;,cookie);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    //允许跳转</span><br><span class="line">    decisionHandler(WKNavigationResponsePolicyAllow);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>打印cookie结果为</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">cookie ========= &lt;NSHTTPCookie</span><br><span class="line">	//	抽象返回接收者的版本。 0版本映射到“老式”的Netscape cookie。版本1映射到RFC2965 cookie。可能会有未来的版本。</span><br><span class="line">	version:0 NSUInteger类型</span><br><span class="line">    </span><br><span class="line">	// 抽象返回接收者的名称。result接收方的名称。</span><br><span class="line">	name:PHPSESSID	 NSString类型</span><br><span class="line">	</span><br><span class="line">	// 抽象返回接收者的值。结果接收器的值。</span><br><span class="line">	value:cg88hphg9ffefi2u8p1cpc8559  NSString类型</span><br><span class="line">	</span><br><span class="line">	// 抽象返回接收者的过期日期。 结果接收者的过期日期。过期日期是cookie应该存在的日期 删除。如果没有特定的过期时间，结果将为nil日期。这只适用于“会话专用”cookie。结果接收者的过期日期。</span><br><span class="line">	expiresDate:&apos;(null)&apos; NSDate类型</span><br><span class="line">	</span><br><span class="line">	//	cookie应该是创建的时间 也就是登录成功的时间</span><br><span class="line">	created:&apos;2018-11-02 02:25:24 +0000&apos; NSDate类型</span><br><span class="line">	</span><br><span class="line">	//	抽象返回接收者是否为会话唯一。如果这个接收者应该在会话结束时被丢弃(不管过期日期)，那么如果接收者不需要在会话结束时被丢弃，则不会。</span><br><span class="line">	sessionOnly:TRUE</span><br><span class="line">	</span><br><span class="line">	//	抽象返回接收方的域。</span><br><span class="line">	//	这个值指定cookie指向的URL域</span><br><span class="line">	//	应该发送。带前导点的域表示cookie</span><br><span class="line">	//	也应该发送到子域，假设有其他的</span><br><span class="line">	//	是有效的限制。有关详细信息，请参阅RFC 2965。</span><br><span class="line">	//	结果是接收器的域。</span><br><span class="line">	domain:shb.ggsw.vip  NSString类型</span><br><span class="line">	</span><br><span class="line">	//	翻译就是分区,</span><br><span class="line">	partition:none</span><br><span class="line">	</span><br><span class="line">	//	抽象返回接收者的路径。</span><br><span class="line">	//	这个值指定cookie下的URL路径</span><br><span class="line">	//	应该为此cookie发送的域。cookie也会</span><br><span class="line">	//	被派去找那个路径的孩子，所以“/”是最一般的。</span><br><span class="line">	//	结果接收器的路径。</span><br><span class="line">	path:/</span><br><span class="line">	</span><br><span class="line">	//	摘要返回是否只发送接收方</span><br><span class="line">	//	安全通道</span><br><span class="line">	//	讨论cookie可以由服务器(或javascript)标记为安全的。</span><br><span class="line">	//	这样标记的cookie只能通过加密连接发送</span><br><span class="line">	//	受信任的服务器(即通过SSL或TLS)，不应该交付给任何服务器</span><br><span class="line">	//	javascript应用程序，以防止跨站点脚本漏洞。</span><br><span class="line">	isSecure:FALSE</span><br><span class="line">	</span><br><span class="line">	//	抽象返回接收者是否应该只发送到HTTP服务器</span><br><span class="line">	//	每RFC 2965</span><br><span class="line">	//	讨论cookie可能被服务器(或javascript)标记为HTTPOnly。</span><br><span class="line">	//	这样标记的cookie只能通过HTTP请求中的HTTP标头发送</span><br><span class="line">	//	对于URL，它匹配相应cookie的路径和域。</span><br><span class="line">	//	具体来说，这些cookie不应该传递给任何javascript</span><br><span class="line">	//	防止跨站点脚本漏洞的应用程序。</span><br><span class="line">	//	如果这个cookie只应该通过HTTP头信息发送，</span><br><span class="line">	//	没有否则。</span><br><span class="line">	isHTTPOnly: YES</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line"> path:&quot;/&quot; isSecure:FALSE isHTTPOnly: YES&gt;</span><br><span class="line">2018-11-02 10:25:24.964388+0800 BHS[1969:198513] cookie ========= &lt;NSHTTPCookie</span><br><span class="line">	version:0</span><br><span class="line">	name:_us</span><br><span class="line">	value:1541211925</span><br><span class="line">	expiresDate:&apos;2028-10-30 02:25:24 +0000&apos;</span><br><span class="line">	created:&apos;2018-11-02 02:25:24 +0000&apos;</span><br><span class="line">	sessionOnly:FALSE</span><br><span class="line">	domain:shb.ggsw.vip</span><br><span class="line">	partition:none</span><br><span class="line">	path:/</span><br><span class="line">	isSecure:FALSE</span><br><span class="line"> path:&quot;/&quot; isSecure:FALSE&gt;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/12/采集视频/" rel="next" title="采集视频">
                <i class="fa fa-chevron-left"></i> 采集视频
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/23/夜间模式/" rel="prev" title="夜间模式">
                夜间模式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#WKWebView"><span class="nav-number">1.</span> <span class="nav-text">WKWebView</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#WKWebViewConfiguration"><span class="nav-number">1.1.</span> <span class="nav-text">WKWebViewConfiguration</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKPreference"><span class="nav-number">1.2.</span> <span class="nav-text">WKPreference</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKProcessPool"><span class="nav-number">1.3.</span> <span class="nav-text">WKProcessPool</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKUserContentController"><span class="nav-number">1.4.</span> <span class="nav-text">WKUserContentController</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKUserScript"><span class="nav-number">1.5.</span> <span class="nav-text">WKUserScript</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKScriptMessageHandler"><span class="nav-number">1.6.</span> <span class="nav-text">WKScriptMessageHandler</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKBackForwardList"><span class="nav-number">1.7.</span> <span class="nav-text">WKBackForwardList</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKBackForwardListItem"><span class="nav-number">1.8.</span> <span class="nav-text">WKBackForwardListItem</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#属性"><span class="nav-number">1.9.</span> <span class="nav-text">属性</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#配置-只读初始化时可以设置"><span class="nav-number">1.9.1.</span> <span class="nav-text">配置,只读初始化时可以设置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#导航代理"><span class="nav-number">1.9.2.</span> <span class="nav-text">导航代理</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#UI代理"><span class="nav-number">1.9.3.</span> <span class="nav-text">UI代理</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#访问过网页历史列表"><span class="nav-number">1.9.4.</span> <span class="nav-text">访问过网页历史列表</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#网页标题"><span class="nav-number">1.9.5.</span> <span class="nav-text">网页标题</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#网页的URL地址"><span class="nav-number">1.9.6.</span> <span class="nav-text">网页的URL地址</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#网页是否正在加载"><span class="nav-number">1.9.7.</span> <span class="nav-text">网页是否正在加载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#加载的进度-范围为-0-1"><span class="nav-number">1.9.8.</span> <span class="nav-text">加载的进度 范围为[0, 1]</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#网页链接是否安全"><span class="nav-number">1.9.9.</span> <span class="nav-text">网页链接是否安全</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#证书服务"><span class="nav-number">1.9.10.</span> <span class="nav-text">证书服务</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#是否可以返回"><span class="nav-number">1.9.11.</span> <span class="nav-text">是否可以返回</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#是否可以前进"><span class="nav-number">1.9.12.</span> <span class="nav-text">是否可以前进</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#自定义代理字符串"><span class="nav-number">1.9.13.</span> <span class="nav-text">自定义代理字符串</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#在iOS上默认为NO，标识不允许链接预览"><span class="nav-number">1.9.14.</span> <span class="nav-text">在iOS上默认为NO，标识不允许链接预览</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#滚动视图"><span class="nav-number">1.9.15.</span> <span class="nav-text">滚动视图</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#是否支持放大手势-默认为NO"><span class="nav-number">1.9.16.</span> <span class="nav-text">是否支持放大手势,默认为NO</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#放大因子，默认为1"><span class="nav-number">1.9.17.</span> <span class="nav-text">放大因子，默认为1</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法"><span class="nav-number">1.10.</span> <span class="nav-text">方法</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#加载webView"><span class="nav-number">1.10.1.</span> <span class="nav-text">加载webView</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#用字符串加载"><span class="nav-number">1.10.2.</span> <span class="nav-text">用字符串加载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#NSData加载"><span class="nav-number">1.10.3.</span> <span class="nav-text">NSData加载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#返回上一个节点"><span class="nav-number">1.10.4.</span> <span class="nav-text">返回上一个节点</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#返回到上一个网页"><span class="nav-number">1.10.5.</span> <span class="nav-text">返回到上一个网页</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#前进到下一个网页"><span class="nav-number">1.10.6.</span> <span class="nav-text">前进到下一个网页</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#重新加载"><span class="nav-number">1.10.7.</span> <span class="nav-text">重新加载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#忽略缓存-重新加载"><span class="nav-number">1.10.8.</span> <span class="nav-text">忽略缓存 重新加载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#停止加载"><span class="nav-number">1.10.9.</span> <span class="nav-text">停止加载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#执行JavaScript"><span class="nav-number">1.10.10.</span> <span class="nav-text">执行JavaScript</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#是否允许左右滑动，返回-前进操作-默认是NO"><span class="nav-number">1.10.11.</span> <span class="nav-text">是否允许左右滑动，返回-前进操作  默认是NO</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#据设置的缩放因子来缩放页面，并居中显示结果在指定的点"><span class="nav-number">1.10.12.</span> <span class="nav-text">据设置的缩放因子来缩放页面，并居中显示结果在指定的点</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#初始化"><span class="nav-number">1.11.</span> <span class="nav-text">初始化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKNavigationDelegate"><span class="nav-number">1.12.</span> <span class="nav-text">WKNavigationDelegate</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#页面开始加载时调用"><span class="nav-number">1.12.1.</span> <span class="nav-text">页面开始加载时调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#当内容开始返回时调用"><span class="nav-number">1.12.2.</span> <span class="nav-text">当内容开始返回时调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#页面加载完成之后调用"><span class="nav-number">1.12.3.</span> <span class="nav-text">页面加载完成之后调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#页面加载失败时调用"><span class="nav-number">1.12.4.</span> <span class="nav-text">页面加载失败时调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#接收到服务器跳转请求之后调用"><span class="nav-number">1.12.5.</span> <span class="nav-text">接收到服务器跳转请求之后调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#在收到响应后-决定是否跳转"><span class="nav-number">1.12.6.</span> <span class="nav-text">在收到响应后,决定是否跳转</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#在发送请求之前-决定是否跳转"><span class="nav-number">1.12.7.</span> <span class="nav-text">在发送请求之前,决定是否跳转</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#点击网页没反应的解决方法"><span class="nav-number">1.12.8.</span> <span class="nav-text">点击网页没反应的解决方法</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKUIDelegate"><span class="nav-number">1.13.</span> <span class="nav-text">WKUIDelegate</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#创建新的webView时调用的方法"><span class="nav-number">1.13.1.</span> <span class="nav-text">创建新的webView时调用的方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#输入框-JavaScript调用prompt方法后回调的方法-prompt是js中的输入框-需要在block中把用户输入的信息传入"><span class="nav-number">1.13.2.</span> <span class="nav-text">输入框,JavaScript调用prompt方法后回调的方法 prompt是js中的输入框 需要在block中把用户输入的信息传入</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#确认框-JavaScript调用confirm方法后回调的方法-confirm是js中的确定框，需要在block中把用户选择的情况传递进去"><span class="nav-number">1.13.3.</span> <span class="nav-text">确认框,JavaScript调用confirm方法后回调的方法 confirm是js中的确定框，需要在block中把用户选择的情况传递进去</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#警告框-web界面中有弹出警告框时调用"><span class="nav-number">1.13.4.</span> <span class="nav-text">警告框,web界面中有弹出警告框时调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#默认浏览元素调用"><span class="nav-number">1.13.5.</span> <span class="nav-text">默认浏览元素调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#关闭WebView时调用"><span class="nav-number">1.13.6.</span> <span class="nav-text">关闭WebView时调用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为"><span class="nav-number">1.13.7.</span> <span class="nav-text">返回一个视图控制器将导致视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#允许应用程序向它创建的视图控制器弹出"><span class="nav-number">1.13.8.</span> <span class="nav-text">允许应用程序向它创建的视图控制器弹出</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#监听加载进度"><span class="nav-number">1.14.</span> <span class="nav-text">监听加载进度</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#WKUserContentController-1"><span class="nav-number">2.</span> <span class="nav-text">WKUserContentController</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#核心两个方法"><span class="nav-number">2.1.</span> <span class="nav-text">核心两个方法</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#向请求的页面中注入JS代码，如果需要在请求的页面中加入自己的JS代码，用此方法"><span class="nav-number">2.1.1.</span> <span class="nav-text">向请求的页面中注入JS代码，如果需要在请求的页面中加入自己的JS代码，用此方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#JavaScript调用OC的桥梁"><span class="nav-number">2.1.2.</span> <span class="nav-text">JavaScript调用OC的桥梁</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKScriptMessage"><span class="nav-number">2.2.</span> <span class="nav-text">WKScriptMessage</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#JS想通知OC只需要"><span class="nav-number">2.2.1.</span> <span class="nav-text">JS想通知OC只需要</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#JS调用OC"><span class="nav-number">2.3.</span> <span class="nav-text">JS调用OC</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#OC调用JS"><span class="nav-number">2.4.</span> <span class="nav-text">OC调用JS</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WKUserScript-JS注入"><span class="nav-number">2.5.</span> <span class="nav-text">WKUserScript JS注入</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#WKUserScriptInjectionTime-枚举-注入的时间"><span class="nav-number">2.5.1.</span> <span class="nav-text">WKUserScriptInjectionTime 枚举 注入的时间</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#WKUserScriptInjectionTimeAtDocumentStart：js加载前执行"><span class="nav-number">2.5.1.1.</span> <span class="nav-text">WKUserScriptInjectionTimeAtDocumentStart：js加载前执行</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#WKUserScriptInjectionTimeAtDocumentEnd：js加载后执行"><span class="nav-number">2.5.1.2.</span> <span class="nav-text">WKUserScriptInjectionTimeAtDocumentEnd：js加载后执行</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#注入"><span class="nav-number">2.5.2.</span> <span class="nav-text">注入</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#注意点"><span class="nav-number">2.6.</span> <span class="nav-text">注意点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#webView中删除广告"><span class="nav-number">2.7.</span> <span class="nav-text">webView中删除广告</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取Cookie"><span class="nav-number">3.</span> <span class="nav-text">获取Cookie</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
